/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ita.model.dao;

import br.ita.model.entities.Login;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author Gustavo
 */
public class LoginDAO implements DAO {
    
    JPAUtil jpautil = new JPAUtil();    
    private EntityManager em = jpautil.getEntityManager();

    @Override
    public void insert(Object o) {
        em.getTransaction().begin();
        em.persist(o);
        em.getTransaction().commit();
    }

    @Override
    public void update(Object o) {
        em.getTransaction().begin();
        em.merge(o);
        em.getTransaction().commit();
    }

    @Override
    public void delete(Object o) {
        em.getTransaction().begin();
        em.remove(retrieve(o));
        em.getTransaction().commit();
    }

    @Override
    public List findAll() {
        List<Login> logins = new ArrayList<Login>();
        Query query =  em.createQuery("SELECT * FROM login");
        logins = query.getResultList();
        return logins; 
    }

    @Override
    public Entity retrieve(Object o) {
        Login resultado = new Login();
        resultado = em.find(Login.class, o);
        return (Entity) resultado;
    }
}